import os

def plot_global_grid():
    # 获取当前脚本所在目录
    base_dir = os.path.dirname(os.path.abspath(__file__))
    csv_file = os.path.join(base_dir, 'global_grid.csv')

    import csv
    import matplotlib.pyplot as plt
    from shapely import wkt
    from shapely.geometry import Polygon

    fig, ax = plt.subplots(figsize=(12, 6))
    with open(csv_file, 'r', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            poly = wkt.loads(row['geom'])
            if isinstance(poly, Polygon):
                x, y = poly.exterior.xy
                ax.plot(x, y, color='blue', linewidth=0.8)
                # 可选：在每个格子中心标注名字
                centroid = poly.centroid
                ax.text(centroid.x, centroid.y, row['name'], fontsize=6, ha='center', va='center')
    ax.set_title('Global Grid Visualization')
    ax.set_xlabel('Longitude')
    ax.set_ylabel('Latitude')
    ax.set_xlim(-180, 180)
    ax.set_ylim(-90, 90)
    ax.set_aspect('auto')
    plt.grid(True, linestyle='--', alpha=0.5)
    plt.show()

if __name__ == "__main__":
    plot_global_grid()